'use strict';

const mongoose = require('mongoose'),
    orderModel = mongoose.model('Order');

module.exports = {
    addOrder: addOrder,
    updateOrder: updateOrder,
    getOrderByMerchant: getOrderByMerchant, 
    getOrderById: getOrderById,
    getUnreceivedOrder: getUnreceivedOrder,
    getReceivedOrder: getReceivedOrder,
    getRefusedOrder: getRefusedOrder,
    getDeliveringOrder: getDeliveringOrder,
    getFinishedOrder: getFinishedOrder,  
}


function addOrder(order,callback){
    orderModel.create(order,function(error,data){
        callback(null,data);
    });
}

function updateOrder(order,callback){
    orderModel.updateOne({_id: order._id},
        {
            foods : order.foods,
            merchant : order.merchant,
            client : order.client,
            deliverInfo : order.deliverInfo,
            compstate : order.compstate,
            ostate : order.ostate,
            otime : order.otime,
            osum  : order.osum,
            fcnt  : order.fcnt,      
        },function(error,data){
        callback(null,data);
    });
    
}

function getOrderByMerchant(icnum,callback){
     orderModel.find({
         'merchant.icnum' : icnum      
     },function(error, data){
         callback(null,data);
     });
}

function getOrderById(_id,callback){
    orderModel.find({'_id':_id},function(error, data){
        callback(null,data);
    });
}

function getUnreceivedOrder(icnum,callback){
         orderModel.find({
         'merchant.icnum' : icnum, 
         ostate : '0'      
     },function(error, data){
         callback(null,data);
     });
}

function getReceivedOrder(icnum,callback){
         orderModel.find({
         'merchant.icnum' : icnum ,
         ostate : '1'      
     },function(error, data){
         callback(null,data);
     });
}

function getRefusedOrder(icnum,callback){
         orderModel.find({
         'merchant.icnum' : icnum ,
         ostate : '2'      
     },function(error, data){
         callback(null,data);
     });
}

function getDeliveringOrder(icnum,callback){
         orderModel.find({
         'merchant.icnum' : icnum ,
         ostate : '3'      
     },function(error, data){
         callback(null,data);
     });
}

function getFinishedOrder(icnum,callback){
         orderModel.find({
         'merchant.icnum' : icnum ,
         ostate : '4'      
     },function(error, data){
         callback(null,data);
     });
}